﻿using System;
using System.Linq;
using ChiWalker.Dynamics;
using ChiWalker.Helpers;
using ChiWalker.Settings;
using ChiWalker.Spell;
using ChiWalker.Utilities;
using Styx;
using Styx.Common.Helpers;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;

namespace ChiWalker.Routines.Druid
{
    internal class Boomking
    {
        private static int _oldDps = Spell.Wrath;

        private static readonly WaitTimer Moonfire = new WaitTimer(TimeSpan.FromSeconds(2));
        private static readonly WaitTimer Sunfire = new WaitTimer(TimeSpan.FromSeconds(2));

        private static int BoomkinDpsSpell
        {
            get
            {
                if (StyxWoW.Me.HasAura("Eclipse (Solar)"))
                    _oldDps = Spell.Wrath;
                else if (StyxWoW.Me.HasAura("Eclipse (Lunar)"))
                    _oldDps = Spell.Starfire;

                return _oldDps;
            }
        }

        private static WoWUnit DotTarget(int missingAura)
        {
            return
                Clusters.NearbyUnitsInCombatWithMe.FirstOrDefault(
                    u => u.InLineOfSight && u.Distance <= 40 &&
                         u.GetAuraTimeLeft(missingAura, true).TotalSeconds <= 2);
        }

        [Behavior(BehaviorType.Combat | BehaviorType.Pull)]
        [Spec(WoWSpec.DruidBalance)]
        [Class(WoWClass.Druid)]
        public static Composite DruidBalance()
        {
            return
                new PrioritySelector(
                    new CastOnSelf(Spell.Rejuvenation,
                        ret => StyxWoW.Me.HealthPercent <= 90 && !StyxWoW.Me.HasAura("Rejuvenation")),
                    new CastOnSelf(Spell.HealingTouch, ret => StyxWoW.Me.HealthPercent <= 50),
                    new CastOnSelf(Spell.Innervate, ret => StyxWoW.Me.ManaPercent < 40),
                    new CastOnSelf(Spell.MoonkinForm, ret => StyxWoW.Me.Shapeshift != ShapeshiftForm.Moonkin),
                    Safers.EnsureTarget(),
                    new CastOnUnit(Spell.Sunfire, ret => DotTarget(Spell.Sunfire),
                        ret => Sunfire.IsFinished && DotTarget(Spell.Sunfire) != null, ret => Sunfire.Reset()),
                    new CastOnUnit(Spell.Moonfire, ret => DotTarget(Spell.Moonfire),
                        ret => Moonfire.IsFinished && DotTarget(Spell.Moonfire) != null,
                        ret => Moonfire.Reset()),
                    Movement.MoveToLoS(ret => StyxWoW.Me.CurrentTarget),
                    Movement.MoveToUnit(ret => StyxWoW.Me.CurrentTarget, ret => 40f),
                    Movement.FaceMyTarget(),
                    new Decorator(ret => StyxWoW.Me.HasAura("Eclipse (Solar)") || StyxWoW.Me.HasAura("Eclipse (Lunar)"),
                        new PrioritySelector(
                            new CastOnSelf(Spell.Incarnation,
                                ret =>
                                    SettingsAnalyizer.AnalizeSpellUsage(
                                        ChiWalkerSettings.Instance.Druid.Incarnation)),
                            new CastOnSelf(Spell.NaturesVigil,
                                ret =>
                                    SettingsAnalyizer.AnalizeSpellUsage(
                                        ChiWalkerSettings.Instance.Druid.NaturesVigil)),
                            new CastOnSelf(Spell.NaturesSwiftness)
                            )),
                    new CastOnSelf(Spell.CelestialAlignment,
                        ret =>
                            SettingsAnalyizer.AnalizeSpellUsage(
                                ChiWalkerSettings.Instance.Druid.CelestialAlignment)),
                    new CastOnSelf(Spell.Starfall,
                        ret => SettingsAnalyizer.AnalizeSpellUsage(ChiWalkerSettings.Instance.Druid.Starfall)),
                    new CastOnMyTarget(Spell.Starsurge),
                    new CastOnMyTarget(Spell.Starfire, ret => BoomkinDpsSpell == Spell.Wrath),
                    new CastOnMyTarget(Spell.Wrath)
                    );
        }
    }
}